﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;

namespace Galaxy.AutoCAD.Editors
{
    public static class SelectionTool
    {
        #region 选择集合对象
        public static DBObjectCollection SelectCollectionDBObject()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            DBObjectCollection dBObjectCollection = new DBObjectCollection();
            PromptSelectionResult psr = ed.GetSelection();
            if (psr.Status == PromptStatus.OK)
            {
                using (Transaction tran = db.TransactionManager.StartTransaction())
                {
                    SelectionSet ss = psr.Value;
                    foreach (ObjectId id in ss.GetObjectIds())
                    {
                        DBObject dBObject = tran.GetObject(id, OpenMode.ForRead, true);
                        if (dBObject != null)
                        {
                            dBObjectCollection.Add(dBObject);
                        }
                    }
                    tran.Commit();
                }
            }
            return dBObjectCollection;
        }
        #endregion
        #region 选择最后创建的对象
        public static DBObjectCollection SelectLastCreateDBObject()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            DBObjectCollection dBObjCol = new DBObjectCollection();
            PromptSelectionResult psr = ed.SelectLast();
            if (psr.Status == PromptStatus.OK)
            {
                using (Transaction tran = db.TransactionManager.StartTransaction())
                {
                    SelectionSet ss = psr.Value;
                    ObjectId[] ids = ss.GetObjectIds();
                    for (int i = 0; i < ids.Length; i++)
                    {
                        ObjectId id = ids[i];
                        DBObject dBObj = tran.GetObject(id, OpenMode.ForWrite, true);
                        if (dBObj != null)
                        {
                            dBObjCol.Add(dBObj);
                        }
                    }
                    tran.Commit();
                }
            }
            return dBObjCol;
        }
        #endregion
        #region 选择矩形框里面的实体(定义为editor括展函数)
        public static DBObjectCollection ChoiceWindowDBObject(this Editor ed, Point3d pt1, Point3d pt2)
        {

            DBObjectCollection dBObjectCollection = new DBObjectCollection();
            PromptSelectionResult psr = ed.SelectCrossingWindow(pt1, pt2);
            if (psr.Status == PromptStatus.OK)
            {
                using (Transaction tran = Application.DocumentManager.MdiActiveDocument.Database.TransactionManager.StartTransaction())
                {
                    SelectionSet value = psr.Value;
                    ObjectId[] objectIds = value.GetObjectIds();
                    foreach (ObjectId id in objectIds)
                    {
                        DBObject dBObject = tran.GetObject(id, OpenMode.ForWrite, true);
                        if (dBObject != null)
                        {
                            dBObjectCollection.Add(dBObject);
                        }
                    }
                    tran.Commit();
                }
            }
            return dBObjectCollection;
        }
        #endregion
        #region 选择单个对象
        public static DBObject ChoiceSingleDBOject(this Editor ed, string message)
        {
            DBObject result = null;
            PromptEntityResult per = ed.GetEntity(message);
            if (per.Status == PromptStatus.OK)
            {
                using (Transaction transaction = ed.Document.Database.TransactionManager.StartTransaction())
                {
                    result = transaction.GetObject(per.ObjectId, OpenMode.ForWrite, true);
                    transaction.Commit();
                }
            }
            return result;
        }
        #endregion 
    }
}
